package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.common.utils.StringUtils;
import com.xc.erp.modules.erp.mapper.JshDepotItemMapper;
import com.xc.erp.modules.erp.mapper.JshMaterialCurrentStockMapper;
import com.xc.erp.modules.erp.mapper.JshMaterialMapper;
import com.xc.erp.modules.erp.domain.pojo.JshMaterial;
import com.xc.erp.modules.erp.domain.pojo.JshMaterialCurrentStock;
import com.xc.erp.modules.erp.service.JshMaterialService;
import com.xc.erp.modules.erp.domain.vo.DepotMaterialVo;
import com.xc.erp.modules.erp.domain.vo.ResultDepotMaterialVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【jsh_material(产品表)】的数据库操作Service实现
 * @createDate 2022-12-10 11:02:32
 */
@Service
public class JshMaterialServiceImpl extends ServiceImpl<JshMaterialMapper, JshMaterial>
        implements JshMaterialService {

    @Autowired
    private JshMaterialMapper jshMaterialMapper;
    @Autowired
    private JshDepotItemMapper jshDepotItemMapper;
    @Autowired
    private JshMaterialCurrentStockMapper jshMaterialCurrentStockMapper;

    @Override
    public List<Long> materialBylikeDepotList(String materialParam) {
        return list(new QueryWrapper<JshMaterial>().or(jshMaterialQueryWrapper -> {
            jshMaterialQueryWrapper.like("model", materialParam).or(jshMaterialQueryWrapper12 -> jshMaterialQueryWrapper12.like("standard", materialParam)).or(jshMaterialQueryWrapper1 -> jshMaterialQueryWrapper1.like("name", materialParam));
        })).stream().map(JshMaterial -> JshMaterial.getId()).collect(Collectors.toList());
    }

    @Override
    public List<ResultDepotMaterialVo> getDepotMaterialList(DepotMaterialVo depotMaterialVo) {
        List<ResultDepotMaterialVo> depotMaterialList = jshMaterialMapper.getDepotMaterialList(depotMaterialVo);
        for (ResultDepotMaterialVo materialVo : depotMaterialList) {
            if (depotMaterialVo.getDepotId() != null) {
                materialVo.setStock(jshMaterialCurrentStockMapper.
                        selectOne(new QueryWrapper<JshMaterialCurrentStock>().eq("depot_id", materialVo.getDepotId()).eq("material_id", materialVo.getMaterialId()).eq("delete_flag", 0)).getCurrentNumber());
            } else {
                BigDecimal stock = new BigDecimal(0.0);
                for (JshMaterialCurrentStock jshMaterialCurrentStock : jshMaterialCurrentStockMapper.selectList(new QueryWrapper<JshMaterialCurrentStock>().eq("material_id", materialVo.getMaterialId()).eq("delete_flag", 0))) {
                    if(jshMaterialCurrentStock.getCurrentNumber()==null)
                        jshMaterialCurrentStock.setCurrentNumber(new BigDecimal(0));
                    stock = stock.add(jshMaterialCurrentStock.getCurrentNumber());
                }
                materialVo.setStock(stock);
            }
            materialVo.setBillPrice(materialVo.getStock().multiply(materialVo.getUnitPrice()));
//            materialVo.setDepotId(depotMaterialVo.getDepotId());
        }
        return depotMaterialList;
    }

    @Override
    public Long getDepotMaterialTotal(DepotMaterialVo depotMaterialVo) {
        return jshMaterialMapper.getDepotMaterialTotal(depotMaterialVo);
    }

    @Override
    public ResultDepotMaterialVo getMaterialByBarCode(String code, Long depotId) {
        ResultDepotMaterialVo materialVo = jshMaterialMapper.getMaterialByBarCode(code);
        JshMaterialCurrentStock jshMaterialCurrentStock = jshMaterialCurrentStockMapper.selectOne(new QueryWrapper<JshMaterialCurrentStock>().eq("depot_id", depotId).eq("material_id", materialVo.getMaterialId()).eq("delete_flag", 0));
        if (null != jshMaterialCurrentStock)
            materialVo.setStock(jshMaterialCurrentStock.getCurrentNumber());
        else
            materialVo.setStock(new BigDecimal(0));
        if (null != materialVo.getUnitPrice())
            materialVo.setBillPrice(materialVo.getStock().multiply(materialVo.getUnitPrice()));
        materialVo.setDepotId(depotId);
        return materialVo;
    }


}




